package cn.qingyun.gis.modules.scenes.job;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.qingyun.gis.modules.scenes.service.IPoiUpdateLogService;
import cn.qingyun.gis.modules.scenes.service.ISchemaService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author LHJ
 * @description
 * @date 2024/10/10 16:58
 */
@Component
@Slf4j
public class UpdatePoiDataHandler {

    @Resource
    private ISchemaService iSchemaService;

    @Resource
    private IPoiUpdateLogService iPoiUpdateLogService;

    @XxlJob("updatePoiDataHandler")
    public void execute() {
        XxlJobHelper.log("UpdatePoiDataHandler 开始备份区划数据表");
        iSchemaService.executeSqlScript(String.format("CREATE TABLE IF NOT EXISTS bak.%s AS SELECT * from public.%s", "poi_scenario", "poi_scenario"));
        XxlJobHelper.log("UpdatePoiDataHandler 删除三天前的备份表");
        iSchemaService.executeSqlScript(String.format("drop table if exists bak.poi_scenario_%s", DatePattern.PURE_DATE_FORMAT.format(DateUtil.offsetDay(DateUtil.date(), -3))));

        XxlJobHelper.log("UpdatePoiDataHandler 开始更新网格数据");
        iPoiUpdateLogService.doUpdateGrid();
        XxlJobHelper.handleSuccess("解析网格成功");
    }
}
